package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.utils.Preferences;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.ninetaildemonfox.utils.AppManager;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.WelcomePresenter;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/17 16:01
 * 功能描述： 欢迎界面
 * 联系方式：1037438704@qq.com
 */
public class WelcomeActivity extends BaseActivity {

    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.text_welcome)
    TextView textWelcome;
    @BindView(R.id.text_invitation)
    TextView textInvitation;
    @BindView(R.id.text_opening_vip)
    TextView textOpeningVip;
    @BindView(R.id.text_chashou)
    TextView textChashou;
    @BindView(R.id.edit_invite_code)
    EditText editInviteCode;
    private WelcomePresenter welcomePresenter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        imageleftFinish.setVisibility(View.VISIBLE);
        textCenter.setText("欢迎");
        welcomePresenter = new WelcomePresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpRequest.POST(me, "Index/isVip"
                , new Parameter()
                        .add("token", token)
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            if (map == null) {
                                return;
                            }
                            if (map.get("code").equals("1")) {
                                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                if (data.get("vip").equals("2")) {
                                    startActivity(new Intent(me, MainActivity.class));
                                }
                            }
                        }
                    }
                });
    }

    @OnClick({R.id.image_left_finish,
            R.id.text_welcome, R.id.text_invitation, R.id.text_opening_vip, R.id.text_chashou})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                //立即开通
                MessageDialog.show(me, "是否确认退出？", "", "确认", "取消")
                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                EMClient.getInstance().logout(true);
                                AppManager.getInstance().killActivity(MainActivity.class);
                                startActivity(new Intent(me, LoginActivity.class));
                                finish();
                                return false;
                            }
                        });
                break;
            case R.id.text_welcome:
                welcomePresenter.getBindInviteCode(token, editInviteCode.getText().toString().trim(), this);
                Preferences.getInstance().commit(me, "user", "invite_code", editInviteCode.getText().toString().trim());
                String string = Preferences.getInstance().getString(me, "user", "invite_code");
                com.orhanobut.logger.Logger.d("===========invite_code获取到的邀请码======="+string);
                break;
            case R.id.text_invitation:
                //申请邀请码
                startActivity(new Intent(WelcomeActivity.this, AforInvitationActivity.class));
                break;
            case R.id.text_opening_vip:
                //购买会员
                VipActivity.start(this, "1",10);
                break;
            case R.id.text_chashou:
                //查看邀请码
                startActivity(new Intent(me, CheckICodeActivity.class));
                break;
            default:
        }
    }

    @Override
    public void onBackPressed() {
        //立即开通
        MessageDialog.show(me, "是否确认退出？", "", "确认", "取消")
                .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                    @Override
                    public boolean onClick(BaseDialog baseDialog, View v) {
                        EMClient.getInstance().logout(true);
                        AppManager.getInstance().killActivity(MainActivity.class);
                        startActivity(new Intent(me, LoginActivity.class));
                        finish();
                        return false;
                    }
                });
    }
}